<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */


function sy_array_map(&$item, $key)
{
  if ($item instanceof ArrayObject) {
    $item = $item->getArrayCopy();
    array_walk_recursive($item, 'sy_array_map');
  }
  
  elseif ($item  instanceof Dictionary) {
    $item = $item->getAll();
    array_walk_recursive($item, 'sy_array_map');
  }
  
  elseif ($item  instanceof DateTime) {
    $item = $item->format('Y-m-d h:i:s');
  }
}

function sy_array_to_options($data, $key, $value)
{
  $options = array();
  
  if (is_array($data)) {
    if (count($data)) {
      foreach ($data as $row) {
        $options[$row[$key]] = $row[$value];
      }
    }
  } else {
    if (count($data)) {
      foreach ($data as $row) {
        $options[$row->$key] = $row->$value;
      }
    }
  }
  
  return $options;
}


/**
 * Recursively strips slashes from all values in an array
 */
function sy_strip_slashes_deep($value)
{
  if (is_array($value)) {
    $return = array_map('sy_strip_slashes_deep', $value);
    return $return;
  }
  else {
    $return = stripslashes($value);
    return $return;
  }
}

/**
 *
 */
function sy_trim($value)
{
  if (! is_string($value) || empty($value)) return $value;
  return trim($value);
}

/**
 * Recursively add slashes from all values in an array
 */
function sy_add_slashes_deep($value)
{
  if (is_array($value)) {
    $return = array_map('sy_add_slashes_deep', $value);
    return $return;
  }
  else {
    $return = addslashes($value);
    return $return;
  }
}

/**
 * outputs an array/object as a preformated (HTML <pre></pre>) string.
 */
function pre()
{
  $args = func_get_args();
  reset($args);
  echo '<div style="text-align:left">';
  while (count($args)) {
    echo '<pre>';
    print_r(array_shift($args));
    echo '</pre>';
  }
  echo '</div>';
}

/**
 * if $param is a valid key in $source, return it's value
 * otherwise return $default, $source can be eather an array or an object
 */
function sy_get_param($source, $param, $default = null, $testEmpty = true)
{
  if ($testEmpty) return ! empty($source[$param]) ? $source[$param] : $default;
  
  return isset($source[$param]) ? $source[$param] : $default;
}

/**
 *
 */
function sy_fix_url($url, $traillingSlash = false)
{
  $url = preg_replace('#\\\+#', '/', $url);
  
  if (! preg_match('|\.[^/]+$|', $url)) {
    if ($traillingSlash) {
      if (! strrpos('/', $url) === 0) $url .= '/';
    }
    elseif (strrpos('/', $url) === 0) {
      $url = substr($url, 0, strlen($url) - 1);
    }
  }
  
  return $url;
}

/**
 *
 */
function sy_add_http($url, $protocol = 'http')
{
  preg_match('#^([a-z]+:/+)?(.*)#', $url, $o);
  $url = $protocol . '://' . sy_get_param($o, 2);
  return $url;
}

/**
 *
 */
function sy_strip_http($url)
{
  preg_match('#^[a-z]+://(.*)#i', $url, $o);
  $url = $o[1];
  return $url;
}

/**
 *
 */
function sy_define_once($name, $value)
{
  if (! defined($name)) {
    define($name, $value);
  }
}

/**
 *
 */
function sy_path_is_absolute($path)
{
  return IS_WIN ? preg_match('#^([a-z]:.+)$#i', $path) : @file_exists($path);
}

/**
 *
 */
function sy_url_is_absolute($url)
{
  return preg_match('|^https?://|i', $url);
}

/**
 *
 */
function sy_fix_path($path, $extension = null)
{
  // remove trailling slashes, duplicate slashes
  $find = array(
    '#\\\#', '#/+$#', '#/+#', '#/+$#' 
  );
  $replace = array(
    '/', '/', '/', '' 
  );
  $path = preg_replace($find, $replace, $path);
  
  // add missing extension
  if (! empty($extension) && ! preg_match('#\.' . $extension . '$#', $path)) {
    $path .= '.' . $extension;
  }
  
  return $path;
}

/**
 *
 */
/*function sy_fix_url($url)
{
  // remove trailling slashes
  $url = preg_replace('#/+$#', '', $url);
  
  return $url;
}*/

/**
 *
 */
function sy_fix_extension($path, $ext)
{
  if (! preg_match('/\.' . addslashes($ext) . '$/i', $path)) {
    $path .= '.' . $ext;
  }
  
  return $path;
}

function br2dt($date)
{
  if (empty($date)) return $date;
  if (preg_match('/^(\d{2})[^\d](\d{2})[^\d](\d{4})(.*)$/', $date, $parts)) {
    $date = $parts[3].'-'.$parts[2].'-'.$parts[1].$parts[4];
  }
  return strtotime($date);
}

function sy_slugify($s)
{
  $s = strip_tags($s);
  $s = preg_replace('/[\s-]+/', '-', $s);
  $s = preg_replace('/[^\d\w-]/', '', $s);
  return $s;
}

?>